package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Notice;
import com.powernode.model.Result;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告业务控制层
 */
@Api(tags = "公告接口管理")
@RequestMapping("shop/notice")
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation("多条件分页查询公告")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<Page<Notice>> loadNoticePage(Page<Notice> page,Notice notice) {
        page = noticeService.page(page,new LambdaQueryWrapper<Notice>()
                .eq(ObjectUtil.isNotNull(notice.getStatus()),Notice::getStatus,notice.getStatus())
                .eq(ObjectUtil.isNotNull(notice.getIsTop()),Notice::getIsTop,notice.getIsTop())
                .like(StringUtils.hasText(notice.getTitle()),Notice::getTitle,notice.getTitle())
                .orderByDesc(Notice::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("新增公告")
    @PostMapping
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public Result<String> saveNotice(@RequestBody Notice notice) {
        boolean saved = noticeService.save(notice);
        return Result.handle(saved);
    }

//    shop/notice/info/2
    @ApiOperation("根据标识查询公告信息")
    @GetMapping("info/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:info')")
    public Result<Notice> loadNoticeInfo(@PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.success(notice);
    }

    @ApiOperation("修改公告信息")
    @PutMapping
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> modifyNotice(@RequestBody Notice notice) {
        boolean updated = noticeService.updateById(notice);
        return Result.handle(updated);
    }

    @ApiOperation("删除公告")
    @DeleteMapping("{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:delete')")
    public Result<String> removeNotice(@PathVariable Long noticeId) {
        boolean removed = noticeService.removeById(noticeId);
        return Result.handle(removed);
    }

    ////////////////////////// 微信小程序 ////////////////////////////////////
//    shop/notice/topNoticeList
    @ApiOperation("查询小程序首页置顶公告列表")
    @GetMapping("topNoticeList")
    public Result<List<Notice>> loadWxTopNoticeList() {
        List<Notice> list = noticeService.queryWxTopNoticeList();
        return Result.success(list);
    }

//    shop/notice/noticeList
    @ApiOperation("查询小程序所有公告列表")
    @GetMapping("noticeList")
    public Result<List<Notice>> loadWxAllNoticeList() {
        List<Notice> list = noticeService.queryWxAllNoticeList();
        return Result.success(list);
    }

//    shop/notice/detail/8
    @ApiOperation("根据标识查询小程序中公告详情")
    @GetMapping("detail/{id}")
    public Result<Notice> loadWxNoticeDetail(@PathVariable Long id) {
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }
}
